package cn.wl.pchart.controller;

import cn.wl.pchart.entity.Dynamic;
import cn.wl.pchart.entity.Infos;
import cn.wl.pchart.entity.Like;
import cn.wl.pchart.entity.User;
import cn.wl.pchart.service.UserService;
import cn.wl.pchart.tools.OperatorCallback;
import cn.wl.pchart.tools.OperatorCore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.Session;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigInteger;

// 用户控制类

// 避免跨域拦截
@CrossOrigin
// 控制器
@RestController
// 访问路径
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    // 登陆接口
    @RequestMapping(value = "/login", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
     /*
            "id": "用户id",
            "password": "用户密码"
         */
    public JSONObject loign(@RequestBody String data) {
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String password = null;
                try {
                    id = rawJson.getString("id");
                    password = rawJson.getString("password");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、password");
                    return returnJson.toString();
                }
                try {
                    // 验证账户是否存在
                    if (userService.verifyExist(id) == 1) {
                        try {
                            // 验证登陆
                            if (password.equals(userService.login(id))) {
                                returnJson.put("code", 0);
                                returnJson.put("msg", "登陆成功！");
                                return returnJson.toString();
                            } else {
                                returnJson.put("code", 5);
                                returnJson.put("msg", "登陆失败！密码错误！");
                                return returnJson.toString();
                            }
                        } catch (Exception e) {
                            System.out.println("登陆操作失败！");
                            e.printStackTrace();
                            returnJson.put("code", 4);
                            returnJson.put("msg", "登陆操作失败！");
                            return returnJson.toString();
                        }

                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "此id不存在！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("验证操作失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "验证操作失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 注册接口
    @RequestMapping(value = "/register", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    /*
            "id": "用户id",
            "password": "用户密码"
         */
    public JSONObject register(@RequestBody String data) {
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String password = null;
                try {
                    id = rawJson.getString("id");
                    password = rawJson.getString("password");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、password");
                    return returnJson.toString();
                }
                try {
                    if (userService.verifyExist(id) == 0) {
                        try {
                            userService.register(id, password);
                            try {
                                if (userService.verifyExist(id) == 1) {
                                    returnJson.put("code", 0);
                                    returnJson.put("msg", "注册成功！");
                                    return returnJson.toString();
                                } else {
                                    returnJson.put("code", 0);
                                    returnJson.put("msg", "注册失败！");
                                    return returnJson.toString();
                                }
                            } catch (Exception e) {
                                System.out.println("验证操作失败！");
                                e.printStackTrace();
                                returnJson.put("code", 5);
                                returnJson.put("msg", "验证操作失败！");
                                return returnJson.toString();
                            }
                        } catch (Exception e) {
                            System.out.println("注册操作失败！");
                            e.printStackTrace();
                            returnJson.put("code", 4);
                            returnJson.put("msg", "注册操作失败！");
                            return returnJson.toString();
                        }

                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "此id已存在！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("验证操作失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "验证操作失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 判断是否关注
    @RequestMapping(value = "/isLike", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject isLike(@RequestBody String data) {

        /*
            id: "用户id",
            like: "判断是否喜欢的id"
         */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String like = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取关注对象id
                    like = rawJson.getString("like");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、like");
                    return returnJson.toString();
                }
                try {
                    returnJson.put("data", userService.isLike(id, like));
                    returnJson.put("code", 0);
                    returnJson.put("msg", "获取关注信息成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取关注信息失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取关注信息失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 关注
    @RequestMapping(value = "/doLike", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject doLike(@RequestBody String data) {

        /*
            "id": "用户id",
            "like": "喜欢的用户id"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String like = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取关注对象id
                    like = rawJson.getString("like");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、like");
                    return returnJson.toString();
                }
                try {
                    userService.doLike(id, like);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "关注成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("关注失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "关注失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 取消关注
    @RequestMapping(value = "/cancelLike", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject cancelLike(@RequestBody String data) {

        /*
            "id": "用户id",
            "like": "喜欢的用户id"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String like = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取关注对象id
                    like = rawJson.getString("like");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、like");
                    return returnJson.toString();
                }
                try {
                    if (id.equals(like))
                    {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "不能对自己取消关注！");
                        return returnJson.toString();
                    }
                    userService.cancelLike(id, like);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "取消关注成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("取消关注失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "取消关注失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 关注列表
    @RequestMapping(value = "/getLikelist", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getLikelist(@RequestBody String data) {

        /*
            "id": "用户id",
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id");
                    return returnJson.toString();
                }
                try {
                    // 关注列表
                    Like like = userService.getLikeList(id, true);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "获取关注列表成功！");
                    returnJson.put("data", like);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取关注列表失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取关注列表失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 粉丝列表
    @RequestMapping(value = "/getFanslist", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getFanslist(@RequestBody String data) {

        /*
            "id": "用户id",
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id");
                    return returnJson.toString();
                }
                try {
                    // 粉丝列表
                    Like like = userService.getLikeList(id, false);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "获取粉丝列表成功！");
                    returnJson.put("data", like);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取粉丝列表失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取粉丝列表失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 用户信息
    @RequestMapping(value = "/getUserInfos", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getUserInfos(@RequestBody String data) {

        /*
            "id": "用户id",
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id");
                    return returnJson.toString();
                }
                try {
                    // 粉丝列表
                    Infos infos = userService.getUserInfos(id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "获取用户信息成功！");
                    returnJson.put("data", infos);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取用户信息失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取用户信息失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 设置用户信息
    @RequestMapping(value = "/setUserInfos", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject setUserInfos(@RequestBody String data) {

        /*
            "id": "用户id",
            "name": "昵称",
            "sex": "性别，0=男，1=女",
            "age": "年龄",
            "constellation": "星座 1-12， 白羊-双鱼",
            "address": "地址",
            "image": "头像的url"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String name = null;
                String sex = null;
                String age = null;
                String constellation = null;
                String address = null;
                String image = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    name = rawJson.getString("name");
                    sex = rawJson.getString("sex");
                    age = rawJson.getString("age");
                    constellation = rawJson.getString("constellation");
                    address = rawJson.getString("address");
                    image = rawJson.getString("image");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、name、sex、age、constellation、address、image");
                    return returnJson.toString();
                }
                try {
                    userService.setUserInfos(id, name, sex, age, constellation, address, image);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "设置用户信息成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("设置用户信息失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "设置用户信息失败！");
                    return returnJson.toString();
                }
            }
        });
    }



}
